package com.winit.openapi.ims.controller;

import javax.annotation.Resource;

import com.winit.ims.spi.singleItem.vo.PrintItemsLabelReturnVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.winit.common.spi.context.CommandContext;
import com.winit.erp.spi.bpartner.vo.Bpartner;
import com.winit.ims.spi.SingleItemService;
import com.winit.ims.spi.singleItem.command.GenerateSingleItemLabelsForApiCommand;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;

/**
 * 单品打印API
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * xiangqi.zhou 	2.0.0  		2017年1月17日 	Created
 *
 * </pre>
 * @since 2.0.0
 */
@Controller
@RequestMapping(value = "/winit/singleitem")
public class SingleItemApiController extends BaseController {

    @Resource
    private SingleItemService singleItemService;

    /**
     * 创建单品标签(v2)
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/label/print/v2")
    public String generateSingleItemLabelsV2(@ModelAttribute RequestMsg requestMsg,
                                           @ModelAttribute ResponseMsg responseMsg) {

        CommandContext ctx = CommandContext.getContext();
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);

        GenerateSingleItemLabelsForApiCommand command = JSON.parseObject(requestMsg.getData().toString(),
            GenerateSingleItemLabelsForApiCommand.class);
        command.setCustomerCode(bpartner.getValue());
        command.setCtx(ctx);

        PrintItemsLabelReturnVo printItemsLabelReturnVo = singleItemService.generateSingleItemLabelsForApiV2(command);
        responseMsg.setData(printItemsLabelReturnVo);
        return SUCCESS;
    }

    /**
     * 创建单品标签
     *
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/label/print")
    public String generateSingleItemLabels(@ModelAttribute RequestMsg requestMsg,
                                           @ModelAttribute ResponseMsg responseMsg) {

        CommandContext ctx = CommandContext.getContext();
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);

        GenerateSingleItemLabelsForApiCommand command = JSON.parseObject(requestMsg.getData().toString(),
                GenerateSingleItemLabelsForApiCommand.class);
        command.setCustomerCode(bpartner.getValue());
        command.setCtx(ctx);

        String label = singleItemService.generateSingleItemLabelsForApi(command);
        responseMsg.setData(label);
        return SUCCESS;
    }

}
